import 'dart:async';

import 'package:flutter/services.dart';

class Flkuaishou {
  static const MethodChannel _channel = MethodChannel('flutter_kuaishou');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<String?> initPlatform() async {
    final String? initPlatform = await _channel.invokeMethod('initPlatform');
    return initPlatform;
  }

  static Future<String?> initPlatformIOS(
      String apiId, String universalLink) async {
    final String? initPlatform = await _channel.invokeMethod(
        'initPlatform', {'registerApp': apiId, 'universalLink': universalLink});
    return initPlatform;
  }

  static Future<String> KuaiShouOpenSDKAuthRequest(dynamic arguments) async {
    _channel.setMethodCallHandler(handler);
    return await _channel.invokeMethod('kuaishouOpenSDKAuthRequest', arguments);
  }

  static StreamController<Map> _locationUpdateStreamController =
      new StreamController.broadcast();

  static Stream<Map> get authUpdate => _locationUpdateStreamController.stream;

  static Future<dynamic> handler(MethodCall call) {
    String method = call.method;

    switch (method) {
      case "ANDROID_KuaiShouOpenSDKAuthRequest":
        {
          print(
              "Android KuaiShouOpenSDKAuthRequest 授权返回:" + "${call.arguments}");
          _locationUpdateStreamController.add(call.arguments);
        }
        break;
      case "ISO_KuaiShouOpenSDKAuthRequest": //IOS 抖音授权返回
        {
          print("IOS KuaiShouOpenSDKAuthRequest 授权返回:" + "${call.arguments}");
          _locationUpdateStreamController.add(call.arguments);
        }
        break;
    }
    return new Future.value("");
  }
}
